package com.zzyl.nursing.controller.member;

import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.QueryDevicePropertyStatusResponseBody;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.framework.config.properties.AliIoTConfigProperties;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.domain.DeviceDataRequest;
import com.zzyl.nursing.domain.DeviceDataResponse;
import com.zzyl.nursing.dto.*;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.FamilyElderMyVo;
import com.zzyl.nursing.vo.LoginVo;
import com.zzyl.nursing.vo.QueryDeviceDataListByDayVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 老人家属Controller
 * 
 * @author ruoyi
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属的接口")
public class FamilyMemberController extends BaseController
{
    @Autowired
    private IFamilyMemberService familyMemberService;

    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;

    @Autowired
    private AliIoTConfigProperties aliIoTConfigProperties;

    @Autowired
    private Client client;

    @PostMapping("/login")
    @ApiOperation("小程序登录")
    public AjaxResult login(@RequestBody UserLoginRequestDto dto){
        LoginVo loginVo = familyMemberService.login(dto);
        return success(loginVo);
    }


    /**
     * 绑定家人
     * @param elder
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("绑定家人")
    public AjaxResult add(@RequestBody AddFamilyMemberRequest elder) {
        familyMemberService.addFamilyMember(elder);
        return AjaxResult.success();
    }

    /**
     * 发送get请求
     */
    @GetMapping("/my")
    public R get(FamilyElderMyVo familyElderMyVo) {
        Long userId = UserThreadLocal.getUserId();
        List<FamilyElderMyVo> familyMemberElders = familyMemberElderMapper.selectListAll(familyElderMyVo, userId);
        return R.ok(familyMemberElders);
    }

    /**
     * 分页查询
     * @param
     * @param
     * @return
     */
    @GetMapping("/list-by-page")
    public R listByPage( @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        System.out.println("pageNum:"+pageNum+"pageSize:"+pageSize+"======================================");
        return R.ok(familyMemberService.selectPage(pageNum,pageSize));

    }

    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation(" 查询健康数据")
    public R<QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData> QueryDevicePropertyStatus(@RequestBody Device device){

        QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData queryDevicePropertyStatusResponseBodyData = familyMemberService.QueryDevicePropertyStatus(device);
        System.out.println(queryDevicePropertyStatusResponseBodyData+"=================================");
        return R.ok(queryDevicePropertyStatusResponseBodyData);
    }
    /**
     * queryDeviceDataListByDay接口是get请求
     */
    @GetMapping("/queryDeviceDataListByDay")
    public R QueryDeviceDataListByDay(@Param("iotId") String iotId,
                                                       @Param("functionId") String functionId,
                                                       @Param("startTime") Long startTime,
                                                       @Param("endTime") Long endTime){
        DeviceDataRequest request = new DeviceDataRequest();
        request.setFunctionId(functionId);
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        request.setIotId(iotId);
        System.out.println(request+"==================================");
        List<QueryDeviceDataListByDayVo> queryDeviceDataListByDayVos = familyMemberService.queryDeviceDataListByDay(request);
        return R.ok(queryDeviceDataListByDayVos);
    }

    /**
     * queryDeviceDataListByWeek接口是get请求
     */
    @GetMapping("/queryDeviceDataListByWeek")
    public R queryDeviceDataListByWeek(@Param("iotId") String iotId,
                                      @Param("functionId") String functionId,
                                      @Param("startTime") Long startTime,
                                      @Param("endTime") Long endTime){
        DeviceDataRequest request = new DeviceDataRequest();
        request.setFunctionId(functionId);
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        request.setIotId(iotId);
        System.out.println(request+"==================================");
        List<QueryDeviceDataListByDayVo> queryDeviceDataListByDayVos = familyMemberService.queryDeviceDataListByDayXiXi(request);
        return R.ok(queryDeviceDataListByDayVos);
    }


    /**
     * deleteById?id=327
     * 解绑家人
     */
    @DeleteMapping("/deleteById")
    public R deleteById(@RequestParam Long id) {
        System.out.println(id+"==================================");
        familyMemberService.deleteByIdelder(id);
        return R.ok();
    }
}
